<?php

namespace app\sys\controller;

use app\common\model\PermissionModel;
use think\Controller;
use think\facade\Response;

class Base extends Controller
{
    public function initialize() {
        parent::initialize();
        //判断是否登录
        $user = session('loginUser');
        if ($user == null) {
            $this->redirect(url('/sys/login/login'));
            return;
        }
        //请求的URL
        $url = $this->request->baseUrl();
        //判断是否以.html结尾
        if (strrpos($url, '.html') > 0) {
            $url = substr($url, 0, strrpos($url, '.html'));
        }
        $ps_model = model('common/PermissionModel');
        $permissions = $ps_model->get_login_user_permissions($user->name);
        if (!in_array($url, $permissions)) {
            if ($this->request->isAjax()) {
                $array = array(
                    'status' => false,
                    'msg' => '没有权限访问该模块:' . $url,
                );
                exit(json_encode($array));
            } else {
                $u = url('/sys/index/index');
                exit("<script type='text/javascript'>alert('没有权限访问该模块');window.location.href = '$u';</script>");
            }
        }
    }
}
